package com.vincent.videocompressor;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;

/**
 * Created by Vincent Woo
 * Date: 2017/8/16
 * Time: 15:15
 */

public class VideoCompress {
    private static final String TAG = VideoCompress.class.getSimpleName();

    public static VideoCompressTask compressVideoHigh(String srcPath, String destPath, CompressListener listener) {
        VideoCompressTask task = new VideoCompressTask(listener, VideoController.COMPRESS_QUALITY_HIGH);
        task.execute(srcPath, destPath);
        return task;
    }

    public static VideoCompressTask compressVideoMedium(String srcPath, String destPath, CompressListener listener) {
        VideoCompressTask task = new VideoCompressTask(listener, VideoController.COMPRESS_QUALITY_MEDIUM);
        task.execute(srcPath, destPath);
        return task;
    }

    public static VideoCompressTask compressVideoLow(String srcPath, String destPath, CompressListener listener) {
        VideoCompressTask task = new VideoCompressTask(listener, VideoController.COMPRESS_QUALITY_LOW);
        task.execute(srcPath, destPath);
        return task;
    }

    public static void compressVideoAuto(String srcPath, String destPath, CompressListener listener) {
        VideoCompressTask task = new VideoCompressTask(listener, -1);
        task.execute(srcPath, destPath);
        //new Thread(new CompressRunable(listener,-1,srcPath,destPath)).start();
    }

    public static void compressVideoCustom(String srcPath, String destPath,int resultWidth, int resultHeight, int resultBitRate, CompressListener listener) {
        CustomVideoCompressTask task = new CustomVideoCompressTask(listener, resultWidth,resultHeight,resultBitRate);
        task.execute(srcPath, destPath);
        //new Thread(new CompressRunable(listener,-1,srcPath,destPath)).start();
    }

    private static class CompressRunable implements Runnable {
        private CompressListener mListener;
        private int mQuality;
        private String srcPath, destPath;

        public CompressRunable(CompressListener mListener, int mQuality, String srcPath, String destPath) {
            this.mListener = mListener;
            this.mQuality = mQuality;
            this.srcPath = srcPath;
            this.destPath = destPath;
        }

        @Override
        public void run() {
            if (mListener != null) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        mListener.onStart();
                    }
                });
            }

            boolean isSucees = false;
            if (mQuality == -1) {
                isSucees = VideoController2.getInstance().convertVideo(srcPath, destPath, new VideoController2.CompressProgressListener() {
                    @Override
                    public void onProgress(float percent) {
                        publishProgress(percent);
                    }
                });
                return;
            }
            isSucees = VideoController.getInstance().convertVideo(srcPath, destPath, mQuality, new VideoController.CompressProgressListener() {
                @Override
                public void onProgress(float percent) {
                    publishProgress(percent);
                }
            });

            if (mListener != null) {
                final boolean finalIsSucees = isSucees;
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        if (finalIsSucees) {
                            mListener.onSuccess();
                        } else {
                            mListener.onFail();
                        }
                    }
                });
            }

        }

        private void publishProgress(final float percent) {
            if (mListener != null) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        mListener.onProgress(percent);
                    }
                });
            }
        }


    }


    private static class VideoCompressTask extends AsyncTask<String, Float, Boolean> {
        private CompressListener mListener;
        private int mQuality;

        public VideoCompressTask(CompressListener listener, int quality) {
            mListener = listener;
            mQuality = quality;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (mListener != null) {
                mListener.onStart();
            }
        }

        @Override
        protected Boolean doInBackground(String... paths) {
            if (mQuality == -1) {
                return VideoController2.getInstance().convertVideo(paths[0], paths[1], new VideoController2.CompressProgressListener() {
                    @Override
                    public void onProgress(float percent) {
                        publishProgress(percent);
                    }
                });
            }
            return VideoController.getInstance().convertVideo(paths[0], paths[1], mQuality, new VideoController.CompressProgressListener() {
                @Override
                public void onProgress(float percent) {
                    publishProgress(percent);
                }
            });
        }

        @Override
        protected void onProgressUpdate(Float... percent) {
            super.onProgressUpdate(percent);
            if (mListener != null) {
                mListener.onProgress(percent[0]);
            }
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            if (mListener != null) {
                if (result) {
                    mListener.onSuccess();
                } else {
                    mListener.onFail();
                }
            }
        }
    }

    private static class CustomVideoCompressTask extends AsyncTask<String, Float, Boolean> {
        private CompressListener mListener;
        private int resultWidth;
        private int resultHeight;
        private int resultBitRate;

        public CustomVideoCompressTask(CompressListener listener, int resultWidth, int resultHeight, int resultBitRate) {
            this.mListener = listener;
            this.resultWidth = resultWidth;
            this.resultHeight = resultHeight;
            this.resultBitRate = resultBitRate;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (mListener != null) {
                mListener.onStart();
            }
        }

        @Override
        protected Boolean doInBackground(String... paths) {
            return VideoController2.getInstance().convertVideo(paths[0], paths[1],resultWidth,resultHeight,resultBitRate, new VideoController2.CompressProgressListener() {
                @Override
                public void onProgress(float percent) {
                    publishProgress(percent);
                }
            });
        }

        @Override
        protected void onProgressUpdate(Float... percent) {
            super.onProgressUpdate(percent);
            if (mListener != null) {
                mListener.onProgress(percent[0]);
            }
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            if (mListener != null) {
                if (result) {
                    mListener.onSuccess();
                } else {
                    mListener.onFail();
                }
            }
        }
    }

    public interface CompressListener {
        void onStart();

        void onSuccess();

        void onFail();

        void onProgress(float percent);
    }
}
